<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20220315
 * Time: 11:05 
 * 用户角色模块
 */
namespace app\admin\model;

use app\admin\model\Model;
use Mscore\Core\Redis;
use Mscore\Core\Lib\SequenceNumber;

class Role extends Model{
	
    private $role  = 'sys_role';
    private $pertable = 'sys_rules';
    private $perroletable = 'sys_role_has';
    private $ident = 'sys_ident';
    private $redis=false;
    private $error=['code'=>0,'msg'=>'fail'];
    private $success=['code'=>200,'msg'=>'success'];
    
    /**
     * 角色标识列表
     * @param array $ident
     * @param array $where $page $page_size
     */
    public function ident($where=null,$page=1,$page_size=10)
    {
        //部门数据
        $ident['datalist'] = Model::name($this->ident)
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('id asc')
            ->select()
            ->toArray();
        //数据总数
        $ident['total']=Model::name($this->ident)
            ->where($where)
            ->count('id');
        return $ident;
    }

    /**
     * 角色标识信息
     * @param array $identinfo
     * @param array $id
     */
    public function identinfo($id,$file='*'){
        return Model::name($this->ident)->where([
            'id'=>$id
        ])->field($file)->find();
    }

    /**
     * 角色标识保存
     * @param array $identsave
     * @param array $where
     */
    public function identsave($data)
    {
        $id = !empty($data['id'])?$data['id']:false;
        
        $saveData = [
            'grade'=>$data['grade'],
            'status'=>$data['status']
        ];
        switch (true) {
            case $id:
                $conut = Model::name($this->ident)->where(' grade="' . $data['grade'].'" and id !='.$id)->count('id');
                if($conut>0){
                    return ['code'=>0,'msg'=>'fail, ident is exist'];
                }
                Model::name($this->ident)->where(' id=' . $id)->update($saveData);
                return $this->success;
                break;
            
            default:
                $conut = Model::name($this->ident)->where(' grade="' . $data['grade'].'"')->count('id');
                if($conut>0){
                    return ['code'=>0,'msg'=>'fail, ident is exist'];
                }
                Model::name($this->ident)->insert($saveData);
                return $this->success;
                break;
        }
        return $this->error;
    }

    /**
     * 角色标识删除
     * @param array $identdel
     * @param array $where
     */
    public function identdel($id)
    {
        //删除角色
        if ($id) {
            Model::name($this->ident)->where('id=' . $id)->update(['dels'=>1]);
            return $this->success;
        }
        return $this->error;
    }
    /**
     * 角色列表
     * @param array $role
     * @param array $where $page $page_size
     */
    public function role($where=null,$page=1,$page_size=10)
    {
        //部门数据
        $role['datalist'] = Model::name($this->role)
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('id asc')
            ->select()
            ->toArray();
        //数据总数
        $role['total']=Model::name($this->role)
            ->where($where)
            ->count('id');
        return $role;
    }

    /**
     * 角色信息
     * @param array $roleinfo
     * @param array $id
     */
    public function roleinfo($id){
        return Model::name($this->role)->where([
            'id'=>$id
        ])->find();
    }

    /**
     * 角色保存
     * @param array $rolesave
     * @param array $where
     */
    public function rolesave($data)
    {
        $id = !empty($data['id'])?$data['id']:false;
        $saveData = [
            'title'=>$data['title'],
            'grade'=>$data['grade'],
            'ranges'=>$data['ranges'],
            'status'=>$data['status']
        ];
        if ($id) {
            Model::name($this->role)->where(' id=' . $id)->update($saveData);
            return $this->success;
        } else {
            Model::name($this->role)->insert($saveData);
            return $this->success;
        }
        return $this->error; 
    }

    /**
     * 角色删除
     * @param array $roledel
     * @param array $where
     */
    public function roledel($id)
    {
        //删除角色
        if ($id) {
            Model::name($this->role)->where('id=' . $id)->update(['dels'=>1]);
            return $this->success;
        }
        return $this->error; 
    }

    /**
     * 权限-角色
     * @param array $roleper 
     * @param array $where 
     */ 
    public function roleper($roleid='',$where=''){

        $datalist = Model::name($this->pertable)
                    ->alias('a')
                    ->join($this->perroletable.' b','a.id=b.rules_id and b.role_id='.$roleid,'left')
                    ->field(' a.id, a.title, a.code, a.url, a.pid, a.operate, b.operate as checkoperate, b.rules_id as sid ')
                    ->where($where)
                    ->select()
                    ->toArray();
        return $datalist;
    }

    /**
     * 保存 权限-角色
     * @param array $rolepersave 
     * @param array $where 
     */ 
    public function rolepersave($data){
        $operate=json_decode($data['check_id'],true); 
        unset($data['check_id']);
        //删除数据
        Model::name($this->perroletable)->where('role_id='.$data['role_id'])->delete();
        //重新写入数据
        if($operate){
           foreach ($operate as $key => $value) {
                if(!empty($value['operate'])){
                    $value['operate'] = json_encode($value['operate'],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
                }else{
                    $value['operate'] = null;
                }
                Model::name($this->perroletable)->insert(['rules_id'=>$value['id'],'role_id'=>$data['role_id'],'operate'=>$value['operate']]);  
            }
            return $this->success;
        }
        return $this->error; 
    }
	
}